<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class FieldController extends Controller
{

    // 获取数据表字段
    /**
     * 1、自己的模型的方法获取
     * 2、模型获取
     * 3、Db类获取
     */


    // 获取数据库字段（字段信息）（模型自定义方法：tableFields）
    public function info()
    {
        $model  = new AdminUser();
        $fields = $model->tableFields();

        return $this->success($fields);
    }


    // 获取数据库字段（field=>中文名）（模型自定义方法：tableFields）
    public function simple()
    {
        $model  = new AdminUser();
        $fields = $model->tableFields();
        $fields = array_column($fields, 'comment', 'field');

        return $this->success($fields);
    }


    // 获取数据库字段（field=>中文名）（模型自带方法：getFields）（没有缓存起来）
    public function model()
    {
        $model  = new AdminUser();
        $fields = $model->getFields();

        return $this->success($fields);
    }

    // 获取数据表字段信息
    public function schema()
    {
        // 获取数据表字段类型（没有缓存）
        //$fields = \think\facade\Db::connect()->getTableFieldsInfo('tp6_admin_user');

        // 获取数据表字段（没有缓存）
        $fields = Db::connect()->getFields('tp6_admin_user');

        return $this->success($fields);
    }

    // 获取数据表的字段信息（用于模型，获取数据，等）
    // http://www.art.com/test/field/fields
    public function fields()
    {
        $db_name = 'tp_slcj2';
        $db_name = 'laravel_slh_test';
        $db_name = 'laravel_fudan';

        $db_name = 'laravel_asia';
        $table   = 'apt_news';
        //$table   = $this->getData('table');
        //$this->throwReturn(empty($table), '请指定查询的数据表！');

        $do_sql = "SELECT `TABLE_NAME` tablename, `COLUMN_NAME` field,DATA_TYPE `type`,IS_NULLABLE is_null ,CHARACTER_MAXIMUM_LENGTH  `length`,NUMERIC_PRECISION num_length,NUMERIC_SCALE num_bit,COLUMN_COMMENT comment,COLUMN_DEFAULT `default` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='".$db_name."' AND TABLE_NAME='".$table."' ";
        $fields = DB::select($do_sql);
        $rule   = $get = [];


        $cline = chr(10).'<br/><br/>'.chr(10).chr(10);
        // 格式化
        //$fields = array_column($fields, null, 'field');
        $fields  = array_column($fields, 'comment', 'field');
        $bangStr = '';
        foreach($fields as $field => $val){
            $bangStr      .= " '".$field."',";
            $rule[$field] = 'required';
            $get[$field]  = '$this->getData("'.$field.'", null, '.'"trim"'.')';
        }
        $result = ['msg' => var_export($fields, 1), 'rule' => $rule,];

        echo json_encode($fields, JSON_UNESCAPED_UNICODE);
        echo $cline;
        echo var_export($fields, 1);
        echo $cline;
        echo '['.$bangStr.']';
        echo $cline;
        echo var_export($rule, 1);
        echo $cline;
        echo var_export($get, 1);
        echo $cline;
        foreach($fields as $field => $val){
            echo '$'.$field.' = $this->getData("'.$field.'", null, \'trim\');'.chr(10);
        }
        echo $cline;
        //return $this->success($result);
    }

    // 所有的字段信息（用于apipost的描述参数）
    // http://www.art.com/test/field/all
    public function all()
    {
        $db_name = 'laravel_asia';
        $do_sql  = "SELECT `TABLE_NAME` tablename, `COLUMN_NAME` field,DATA_TYPE `type`,IS_NULLABLE is_null ,CHARACTER_MAXIMUM_LENGTH  `length`,NUMERIC_PRECISION num_length,NUMERIC_SCALE num_bit,COLUMN_COMMENT comment,COLUMN_DEFAULT `default` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='".$db_name."'";
        $fields  = DB::select($do_sql);

        // 格式化
        //$fields = array_column($fields, null, 'field');
        $fields = array_column($fields, 'comment', 'field');

        echo json_encode($fields, JSON_UNESCAPED_UNICODE);

        //echo var_export($fields,1);

        return $this->success($fields);
    }

}
